package name.matthewgreet.example3.common; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; /** * <P>If the action implements {@link FormDriven}, stores the form in the session so it may be retrieved by a later * action that displays it. It is usually part of the aflUpdateStack interceptor stack and retrieved by * {@link FormRetrieveInterceptor} as part of the aflViewStack interceptor stack.</P> * * * <H3>Interceptor parameters</H3> * <P>none</P> * * <H3>Extending the interceptor</H3> * <P>This can't be usefully extended.</P> * * <H3>Example code</H3> * <PRE> * @InterceptorRefs({ * @InterceptorRef(value="formStore"), * @InterceptorRef(value="basicStack") *}) * </PRE> */ public class FormStoreInterceptor extends AbstractInterceptor { public static class StoredForm { private String owningURL; private Object form; private boolean invalid; private Class<?> processor; /** * Returns URL of page that retrieved messages so refresh still gets them. */ public String getOwningURL() { return owningURL; } public void setOwningURL(String value) { this.owningURL = value; } public Object getForm() { return form; } public void setForm(Object form) { this.form = form; } public boolean getInvalid() { return invalid; } public void setInvalid(boolean invalid) { this.invalid = invalid; } public Class<?> getProcessor() { return processor; } public void setProcessor(Class<?> processor) { this.processor = processor; } } private static final long serialVersionUID = -2270259451208908292L; public static final String SESSION_STORED_FORM = FormStoreInterceptor.class + "_STORED_FORM"; public FormStoreInterceptor() { // Empty } @Override public String intercept(ActionInvocation invocation) throws Exception { FormDriven<?> formDriven; StoredForm storedForm; String result; result = invocation.invoke(); if (invocation.getAction() instanceof FormDriven) { formDriven = (FormDriven<?>)invocation.getAction(); if (formDriven.getModel() != null && formDriven.getModel().getClass() != NullForm.class) { storedForm = new StoredForm(); storedForm.setForm(formDriven.getModel()); storedForm.setInvalid(formDriven.formValidationFailed()); storedForm.setProcessor(invocation.getAction().getClass()); ActionContext.getContext().getSession().put(SESSION_STORED_FORM, storedForm); } else { ActionContext.getContext().getSession().put(SESSION_STORED_FORM, null); } } return result; } }